package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.DeleteEnum;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizHot;
import com.member.shop.domain.SysBizMarketPage;
import com.member.shop.service.ISysBizHotService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【热门搜索】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-12-17
 **/
@RestController
@RequestMapping("/shop/hot")
public class SysBizHotController extends BaseController {
    @Resource
    private ISysBizHotService sysBizHotService;

    /**
     * 查询热门搜索列表
     */
    @PreAuthorize("@ss.hasPermi('shop:hot:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizHot sysBizHot) {
        setTenantId(sysBizHot);
        startPage();
        LambdaQueryWrapper<SysBizHot> queryWrapper = new LambdaQueryWrapper<SysBizHot>();
        queryWrapper.like(!StringUtils.isEmpty(sysBizHot.getName()), SysBizHot::getName, sysBizHot.getName());
        queryWrapper.eq((sysBizHot.getTenantId()!=null && sysBizHot.getTenantId()!=0),SysBizHot::getTenantId, sysBizHot.getTenantId());
        queryWrapper.eq(SysBizHot::getDeleted, DeleteEnum.NOT_DELETE.getKey());
        List<SysBizHot> list = sysBizHotService.selectSysBizHotList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出热门搜索列表
     */
    @PreAuthorize("@ss.hasPermi('shop:hot:export')")
    @Log(title = "热门搜索", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizHot sysBizHot) {
        LambdaQueryWrapper<SysBizHot> queryWrapper = new LambdaQueryWrapper<SysBizHot>();
        List<SysBizHot> list = sysBizHotService.selectSysBizHotList(queryWrapper);
        ExcelUtil<SysBizHot> util = new ExcelUtil<SysBizHot>(SysBizHot.class);
        util.exportExcel(response, list, "热门搜索数据");
    }

    private void setTenantId(SysBizHot sysBizHot) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizHot.setTenantId(tenantId);
        }
    }

    /**
     * 获取热门搜索详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:hot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizHotService.selectSysBizHotById(id));
    }

    /**
     * 新增热门搜索
     */
    @PreAuthorize("@ss.hasPermi('shop:hot:add')")
    @Log(title = "热门搜索", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizHot sysBizHot) {
        setTenantId(sysBizHot);
        return toAjax(sysBizHotService.insertSysBizHot(sysBizHot));
    }

    /**
     * 修改热门搜索
     */
    @PreAuthorize("@ss.hasPermi('shop:hot:edit')")
    @Log(title = "热门搜索", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizHot sysBizHot) {
        return toAjax(sysBizHotService.updateSysBizHot(sysBizHot));
    }

    /**
     * 删除热门搜索
     */
    @PreAuthorize("@ss.hasPermi('shop:hot:remove')")
    @Log(title = "热门搜索", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(sysBizHotService.deleteSysBizHotById(id));
    }
}
